package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.SelectExam;
import com.ysd.twocrm.entity.Student;
import com.ysd.twocrm.entity.StudentProble;
import com.ysd.twocrm.mapper.ProblemsetMapper;
import com.ysd.twocrm.mapper.SelectExamMapper;
import com.ysd.twocrm.mapper.StudentMapper;
import com.ysd.twocrm.mapper.StudentProbleMapper;
import com.ysd.twocrm.utils.LayuiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

//考试表
@RestController
@RequestMapping("/exam")
public class ExamController {
@Autowired
private SelectExamMapper selectExamMapper;
@Autowired
private ProblemsetMapper problemsetMapper;
@Autowired
private StudentMapper studentMapper;
@Autowired
private StudentProbleMapper studentProbleMapper;

    /*
     * @Description 查询全部考试相关信息，用于班级列表渲染
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/17 9:05
     */
@GetMapping
public Object getAllProblemsetMapper(){


    return problemsetMapper.selectList(null);
}
    /*
     * @Description 根据考试id来查询考试下的全部信息
     * @param pid
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/17 15:25
     */
@GetMapping("/1")
public Object getAllExam(Integer pid){

        QueryWrapper<SelectExam> queryWrapper=new QueryWrapper<SelectExam>();
        queryWrapper.eq("pid",pid);

    return selectExamMapper.selectList(queryWrapper);
}
    /*根据用户id 先去查询学生的信息，然后可以根据学生id和考试id来修改分数和状态
     * @Description 学生在进行考试的时候，会将考试答案和考试的id发送过来然后进来判断当前考了多少分返回给他
     * 并且修改当前学生的考试状态
     * @param pid
     * @param arr
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/17 15:28
     */
@GetMapping("/2")
public Object getAllIfScore(Integer pid,String arr,Integer userId){
    Student studentByUserId = studentMapper.findStudentByUserId(userId);//根据用户id来查询了学生信息
    QueryWrapper<StudentProble> studentProbleQueryWrapper=new QueryWrapper<StudentProble>();
    studentProbleQueryWrapper.eq("pid",pid).eq("studentId",studentByUserId.getStudentId());
    StudentProble studentProble = studentProbleMapper.selectOne(studentProbleQueryWrapper);//根据pid和studentid查询是否存在
    String[] split = arr.split(",");
    //先查询当前考试信息里的全部信息
    QueryWrapper<SelectExam> queryWrapper=new QueryWrapper<SelectExam>();
    queryWrapper.eq("pid",pid);
    List<SelectExam> selectExams = selectExamMapper.selectList(queryWrapper);
    int sum=0;
    for(int i=0;i<selectExams.size();i++){
        if(selectExams.get(i).getAnswer().equals(split[i])){
            System.out.println("成功了>"+selectExams.get(i).getSid());
            sum+=selectExams.get(i).getSnum();
        }
    }
    studentProbleMapper.updateProbleExam(sum,1,studentProble.getSpid());//修改分数和状态
    return sum;
}
    /*
     * @Description
     *  前台发送过来的是一个用户id，所以我们要通过这个用户id去查询学生信息，如果他是学生并且有了，那就查出来显示数据了
     * 根据学生id来查询当前学生以及完成的考试
     * @param studentId
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/17 15:31
     */
@GetMapping("/3")
public Object getAllFindExam(Integer studentId,Integer status){
    Student studentByUserId = studentMapper.findStudentByUserId(studentId);
    QueryWrapper<StudentProble> queryWrapper=new QueryWrapper<StudentProble>();
    queryWrapper.eq("studentId",studentByUserId.getStudentId());
    queryWrapper.eq("status",status);

    LayuiUtils<StudentProble> studentProbleLayuiUtils=new LayuiUtils<>();
    studentProbleLayuiUtils.setData( studentProbleMapper.selectList(queryWrapper));
    return studentProbleLayuiUtils;
}
    /*
     * @Description 根据pid来查询当前考试的名称
     * @param pid
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/17 15:41
     */
    @GetMapping("/4")
public Object getAllExamName(Integer pid){
    return problemsetMapper.selectById(pid);
}


    /*
     * @Description  先去判断当前班级下的学生的考试时间是否结束，如果没有结束的话就返回nofalse
     * 添加，首先先查询当前班级下的全部学生，接着循环查出学生信息，接着添加到学生考试中间表内
     * @param classId
     * @param pid
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/4/17 11:06
     */
@PostMapping
public String insertStudentProble(Integer classId,Integer pid){

    //判断当前班级下的学生的考试时间是否结束
    List<StudentProble> allProbleByStuClassId = studentProbleMapper.findAllProbleByStuClassId(classId);
    for(int j=0;j<allProbleByStuClassId.size();j++){
        long time = allProbleByStuClassId.get(j).getEndtime().getTime();//考试结束时间
        long time1 = new Date().getTime();//当前时间
        if(time1<time){
            return "nofalse";
        }else{

        }

    }
    QueryWrapper<Student> queryWrapper=new QueryWrapper<Student>();
    queryWrapper.eq("stuClassId",classId);
    List<Student> allStudent = studentMapper.selectList(queryWrapper);
    for(int i=0;i<allStudent.size();i++){
        QueryWrapper<StudentProble> studentProbleQueryWrapper=new QueryWrapper<StudentProble>();
        studentProbleQueryWrapper.eq("pid",pid).eq("studentId",allStudent.get(i).getStudentId());
        StudentProble studentProble = studentProbleMapper.selectOne(studentProbleQueryWrapper);//根据pid和studentid查询是否存在
         if(studentProble!=null){
             return "false";
         }

//准备添加
        Calendar calendar= Calendar.getInstance();
        calendar.add(Calendar.HOUR,2);//延迟当前时间两个小时
        Date time = calendar.getTime();
        StudentProble sp=new StudentProble(0,allStudent.get(i).getStudentId(),pid,null,0,new Date(),time,null,null);
        studentProbleMapper.insert(sp);

    }
    return "success";
}

}
